package com.geeke.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.geeke.common.controller.BaseController;
import com.alibaba.fastjson.JSONObject;
import com.geeke.common.data.Page;
import com.geeke.common.data.SearchParams;
import com.geeke.sys.entity.Post;
import com.geeke.sys.service.PostService;
import com.geeke.utils.ResultUtil;

/**
 * 岗位表 Controller
 * @author
 * @version
 */
@RestController
@RequestMapping(value = "/sys/post")
public class PostController extends BaseController {

    @Autowired
    private PostService postService;

    @GetMapping("/{id}")
    public ResponseEntity<JSONObject> getById(@PathVariable("id") String id) {
        Post entity = postService.get(id);
        return ResponseEntity.ok(ResultUtil.successJson(entity));
    }

    @PostMapping(value = { "list", "" })
    public ResponseEntity<JSONObject> listPage(@RequestBody SearchParams searchParams) {
        Page<Post> result = postService.listPage(
            searchParams.getParams(),
            searchParams.getOffset(),
            searchParams.getLimit(),
            searchParams.getOrderby()
        );
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "listAll")
    public ResponseEntity<JSONObject> listAll(@RequestBody SearchParams searchParams) {
        List<Post> result = postService.listAll(searchParams.getParams(), searchParams.getOrderby());
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "save")
    @RequiresPermissions(value={"post:create","post:update"}, logical= Logical.OR)
    public ResponseEntity<JSONObject> save(@RequestBody Post post) {
        String id = postService.save(post).getId();
        return ResponseEntity.ok(ResultUtil.successJson(id));
    }

    @PostMapping(value = "delete")
    @RequiresPermissions("post:delete")
    public ResponseEntity<JSONObject> delete(@RequestBody Post post) {
        int rows = postService.delete(post);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    @PostMapping(value = "bulkInsert")
    @RequiresPermissions("post:create")
    public ResponseEntity<JSONObject> bulkInsert(@RequestBody List<Post> posts) {
        List<String> ids = postService.bulkInsert(posts);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkUpdate")
    @RequiresPermissions("post:update")
    public ResponseEntity<JSONObject> bulkUpdate(@RequestBody List<Post> posts) {
        List<String> ids = postService.bulkUpdate(posts);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkDelete")
    @RequiresPermissions("post:delete")
    public ResponseEntity<JSONObject> bulkDelete(@RequestBody List<Post> posts) {
        int rows = postService.bulkDelete(posts);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }
} 